// DcSdr javascript

var sock = null;
var h_plot = null;
var v_plot = null;

//==============================================================================
// Document load
$(document).ready(function() {
        // Create mode buttons
        $( "#mode" ).buttonset();
        // Bind mode buttons
        $('input[name=mode]:radio').click(function() {
                //console.log($(this));
                sendMsg($.toJSON({"mode":$(this).attr("id")}));
        });
        // Create filter buttons
        $( "#filter" ).buttonset();
        // Bind filter buttons
        $('input[name=filter]:radio').click(function() {
                //console.log($(this));
                sendMsg($.toJSON({"filter":$(this).attr("id")}));
        });
        // Bind vfo mouse events
        $('span').mouseenter(function() {
                //console.log("Enter");
        });
        $('span').mouseleave(function() {
                //console.log("Leave");
        });
        jQuery(function($) {
                $('span')
                .bind('mousewheel', function(event, delta) {
                    var dir = delta > 0 ? 'Up' : 'Down',
                        vel = Math.abs(delta);
                    //console.log(dir + ' at a velocity of ' + vel);
                    sendMsg($.toJSON({"freq":$(this).attr("id"), "delta":delta}));
                    return false;
                });
        });

});
//==============================================================================
// Message execution
function updatefreq(freq) {
        $( "#100MHz" ).html(freq[0]);
        $( "#10MHz" ).html(freq[1]);
        $( "#1MHz" ).html(freq[2]);
        $( "#100KHz" ).html(freq[4]);
        $( "#10KHz" ).html(freq[5]);
        $( "#1KHz" ).html(freq[6]);
        $( "#100Hz" ).html(freq[7]);
        $( "#10Hz" ).html(freq[8]);
        $( "#1Hz" ).html(freq[9]);
}

function updatepan(data) {
        //console.log(data.length);
        var pan = document.getElementById('pan');
        var ctx = pan.getContext('2d');
        ctx.strokeStyle = "green";
        ctx.fillStyle = "black";
        ctx.font = "bold 12px sans-serif";
        ctx.clearRect(0,0,1009,310); 
        ctx.fillRect(0,0,1009,310);
        ctx.fillStyle = "yellow";
        ctx.beginPath();
        updatehplot(ctx, h_plot);
        updatevplot(ctx, v_plot); 
        ctx.moveTo(0, parseInt(data[1]));
        //console.log("Move: 0, " + parseInt(data[1]));
        var i = 0;
        for(i=2;i<=1009;i++) {
                ctx.lineTo(i, parseInt(data[i]));
                //console.log("Plot: " + parseInt(i) + ", " +  parseInt(data[i]));
        }    
        ctx.stroke();
}

function updatehplot(ctx, data) {
        //console.log(data);
        // [tag, db, legend start, line start, line end, ...]
        var i = 0;
        for(i=1;i<(data.length-1);i += 7) {
                ctx.fillText(data[i], parseInt(data[i+1]), parseInt(data[i+2]));
                ctx.moveTo(parseInt(data[i+3]), parseInt(data[i+4]));
                ctx.lineTo(parseInt(data[i+5]), parseInt(data[i+6]));
        }
}

function updatevplot(ctx, data) {
        //console.log("Update v_plot");
        // [tag, freq, legend start, line start, line end, ...] 
        var i = 0;
        for(i=1;i<(data.length-1);i += 7) {
                ctx.fillText(data[i], parseInt(data[i+1]), parseInt(data[i+2]));
                ctx.moveTo(parseInt(data[i+3]), parseInt(data[i+4]));
                ctx.lineTo(parseInt(data[i+5]), parseInt(data[i+6]));
        }
}

//==============================================================================
// Web sockets interface
sock = new WebSocket("ws://"+window.location.hostname+":9999/");
sock.onopen = function(e) {
        console.log("opened");
}
sock.onclose = function(e) {
        console.log("closed");
}
sock.onmessage = function(e) {
        //console.log("got: " + e.data);
        var data = $.evalJSON(e.data);
        //console.log("converted: " + msg[0], msg[1]);
        if(data[0] == "freq") {
                updatefreq(data[1]);
        }
        else if(data[0] == "pan") {
                updatepan(data);
        }
        else if(data[0] == "h_plot") {
                h_plot = data;
        }
        else if(data[0] == "v_plot") {
                v_plot = data;
        }
}

function sendMsg(msg) {
        console.log(sock, msg);
        sock.send(msg);
}

